package com.btobtob.a.biz.user.config;

import com.btobtob.a.biz.user.constant.HttpHeaderConstant;
import io.swagger.v3.oas.annotations.servers.Servers;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import lombok.Value;
import org.springdoc.core.configuration.SpringDocConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.filter.ForwardedHeaderFilter;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

@Configuration
@Import(SpringDocConfiguration.class)
public class SpringDocConfig {

    @Resource
    private Environment environment;

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("API文档")
                        .version("1.0")
                        .contact(new Contact().name("").email("")))
                .addSecurityItem(new SecurityRequirement().addList(HttpHeaderConstant.ADMIN_X_AUTH_Token))
                .addServersItem(new Server().url("http://192.168.1.201:40000/j-user").description("带网关"))
                .addServersItem(new Server().url("http://localhost:" + environment.getProperty("server.port")).description("不带网关"))
                .components(new Components()
                        .addSecuritySchemes(HttpHeaderConstant.X_AUTH_Token,
                                new SecurityScheme()
                                        .type(SecurityScheme.Type.APIKEY)
                                        .name(HttpHeaderConstant.X_AUTH_Token)
                                        .in(SecurityScheme.In.HEADER)
                                        .description(HttpHeaderConstant.X_AUTH_Token)
                        )
                        .addSecuritySchemes(HttpHeaderConstant.ADMIN_X_AUTH_Token,
                                new SecurityScheme()
                                        .type(SecurityScheme.Type.APIKEY)
                                        .name(HttpHeaderConstant.ADMIN_X_AUTH_Token)
                                        .in(SecurityScheme.In.HEADER)
                                        .description(HttpHeaderConstant.ADMIN_X_AUTH_Token)
                        )
                );
    }

    @Bean
    ForwardedHeaderFilter forwardedHeaderFilter() {
        return new ForwardedHeaderFilter();
    }
}
